package com.hkts.beltweigher.master.service.serviceimpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hkts.beltweigher.master.entity.ScaleData;
import com.hkts.beltweigher.master.entity.YbClassesAccumulate;
import com.hkts.beltweigher.master.entity.YbHourAccumulate;
import com.hkts.beltweigher.master.mapper.YbHourAccumulateMapper;
import com.hkts.beltweigher.master.service.IYbHourAccumulateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hkts.beltweigher.master.vo.PageParam;
import com.hkts.beltweigher.master.vo.PageVo;
import com.hkts.beltweigher.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hml
 * @since 2024-04-10
 */
@Service
@Transactional
public class YbHourAccumulateServiceImpl extends ServiceImpl<YbHourAccumulateMapper, YbHourAccumulate> implements IYbHourAccumulateService {


    @Autowired
    private YbHourAccumulateMapper ybHourAccumulateMapper;

    @Override
    public IPage<YbHourAccumulate> selectPage(Page<YbHourAccumulate> page, YbHourAccumulate ybHourAccumulate) {
        QueryWrapper<YbHourAccumulate> query = Wrappers.query();
        return ybHourAccumulateMapper.selectPage(page, query);
    }


    @Override
    public List<YbHourAccumulate> selectList(YbHourAccumulate ybHourAccumulate) {
        QueryWrapper<YbHourAccumulate> query = Wrappers.query();
        return ybHourAccumulateMapper.selectList(query);
    }

    @Override
    public YbHourAccumulate selectYbHourAccumulate() {
        QueryWrapper<YbHourAccumulate> query = Wrappers.query();
        query.lambda().last("LIMIT 1").orderByDesc(YbHourAccumulate::getId);
        return ybHourAccumulateMapper.selectOne(query);
    }


    public Map<String, Object> getChar(PageVo vo) {
        PageParam pageParam = new PageParam();
        pageParam.setPageNumber(vo.getPageNumber());
        pageParam.setPageSize(vo.getPageSize());
        pageParam.setStartTime(vo.getStartTime());
        pageParam.setEndTime(vo.getEndTime());
        pageParam.setStartRow(vo.getStartRow());
        List<YbHourAccumulate> aChar = ybHourAccumulateMapper.getChar(pageParam);
        Map<String, Object> result = new HashMap<String, Object>();
        if (aChar.size() > 0){
            List<Float> strListZw1 = new ArrayList<>();
            List<Float> strListZw2 = new ArrayList<>();
            List<Float> strListZw3 = new ArrayList<>();
            List<Float> strListZw4 = new ArrayList<>();
            List<Float> strListZw5 = new ArrayList<>();
            List<String> times = new ArrayList<>();
            for (int i = 0; i < aChar.size(); i++) {
                if (aChar.get(i).getInstrumentName().equals("中文仪表1")){
                    strListZw1.add(aChar.get(i).getTotal());
                }else if (aChar.get(i).getInstrumentName().equals("中文仪表2")){
                    strListZw2.add(aChar.get(i).getTotal());
                }else if (aChar.get(i).getInstrumentName().equals("中文仪表3")){
                    strListZw3.add(aChar.get(i).getTotal());
                }else if (aChar.get(i).getInstrumentName().equals("中文仪表4")){
                    strListZw4.add(aChar.get(i).getTotal());
                }else if (aChar.get(i).getInstrumentName().equals("中文仪表5")){
                    strListZw5.add(aChar.get(i).getTotal());
                }
                String strDateHH = DateUtil.getStrDateHH(aChar.get(i).getEndTime());
                times.add(strDateHH);
            }

            Map<String,Object> series = new HashMap<>();
            series.put("中文仪表1",strListZw1);
            series.put("中文仪表2",strListZw2);
            series.put("中文仪表3",strListZw3);
            series.put("中文仪表4",strListZw4);
            series.put("中文仪表5",strListZw5);
            TreeMap<String, Object> stringObjectTreeMap = new TreeMap<>(series);
            List<String> collect = aChar.stream().map(YbHourAccumulate::getInstrumentName).distinct().collect(Collectors.toList());
            List<String> timesDis = times.stream().sorted().distinct().collect(Collectors.toList());

            result.put("series",stringObjectTreeMap);
            result.put("times",timesDis);
            result.put("list", collect);
            result.put("totalRows", aChar.size());
            result.put("pageSize", vo.getPageSize());
            result.put("pageNumber", vo.getPageNumber());
            return result;
        }else {
            result.put("series", new ArrayList<>());
            result.put("list", new ArrayList<>());
            result.put("totalRows", aChar.size());
            result.put("pageSize", vo.getPageSize());
            result.put("pageNumber", vo.getPageNumber());
            return result;
        }
    }
}
